﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace UniRlv.VisionScapeAdapter
{
    public class IniFileRW
    {
        private static string CFG_FILE = "VisionScapeAdapter.ini";

        #region API函数声明

        [DllImport("kernel32")]//返回0表示失败，非0为成功
        private static extern long WritePrivateProfileString(string section, string key,
            string val, string filePath);

        [DllImport("kernel32")]//返回取得字符串缓冲区的长度
        private static extern long GetPrivateProfileString(string section, string key,
            string def, StringBuilder retVal, int size, string filePath);

        #endregion

        #region 读写Ini文件通用API

        public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                StringBuilder temp = new StringBuilder(1024);
                GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
                return temp.ToString();
            }
            else
            {
                return String.Empty;
            }
        }

        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }

        #endregion

        #region 常用API
        public static string ReadString(string section, string key, string def)
        {
            string iniPath = AppDomain.CurrentDomain.BaseDirectory + CFG_FILE;
            return ReadIniData(section, key, def, iniPath);
        }

        public static int ReadInt(string section, string key, int def)
        {
            string ret = ReadString(section, key, def.ToString());

            if (string.IsNullOrEmpty(ret))
            {
                return def;
            }
            else
            {
                try
                {
                    return Convert.ToInt32(ret);
                }
                catch (Exception)
                {
                    return def;
                }
                
            }
        }

        public static bool ReadBoolean(string section, string key, bool def)
        {
            string ret = ReadString(section, key, def.ToString());

            if (string.IsNullOrEmpty(ret))
            {
                return def;
            }
            else
            {
                try
                {
                    return Convert.ToBoolean(ret);
                }
                catch (Exception)
                {
                    return def;
                }
                
            }
        }

        public static double ReadDouble(string section, string key, double def)
        {
            string ret = ReadString(section, key, def.ToString());

            if (string.IsNullOrEmpty(ret))
            {
                return def;
            }
            else
            {
                try
                {
                    return Convert.ToDouble(ret);
                }
                catch (Exception)
                {
                    return def;
                }
                
            }
        }

        public static bool WriteString(string section, string key, string val)
        {
            string iniPath = AppDomain.CurrentDomain.BaseDirectory + CFG_FILE;
            return WriteIniData(section, key, val, iniPath);
        }

        public static bool WriteInt(string section, string key, int val)
        {
            return WriteString(section, key, val.ToString());
        }

        public static bool WriteBoolean(string section, string key, bool val)
        {
            return WriteString(section, key, val.ToString());
        }

        public static bool WriteDouble(string section, string key, double val)
        {
            return WriteString(section, key, val.ToString());
        }
        #endregion
    }
}
